<style scoped>
.img-face {
    max-width: 200px;
    max-height: 200px;
    padding: 10px;
    cursor: pointer;
}

.split {
    height: calc(100vh - 140px);
    background: #ffffff;
    border: 1px solid #dcdee2;
}

.split-pane {
    padding: 10px;
}

/deep/.ivu-table-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}

/deep/.ivu-table-fixed-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}
</style>
<template>
    <div class="split">
        <div class="split-pane">
            <Card style="margin-bottom: 10px">
                <Form inline class="form-search">
                    <FormItem>
                        <DatePicker style="width: 150px" type="date" format="yyyy年MM月dd日" placeholder="开始日期" :clearable="true" v-model="sdate"></DatePicker>
                    </FormItem>
                    <FormItem>
                        <DatePicker style="width: 150px" type="date" format="yyyy年MM月dd日" placeholder="结束日期" :clearable="true" v-model="edate"></DatePicker>
                    </FormItem>
                    <FormItem></FormItem>
                    <FormItem>
                        <Input v-model="keyword" placeholder="人员名称/药品" style="width: 200px" />
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="
                            page = 1;
                        SearchSaleOrder();" icon="md-search">查询</Button>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="AddData()" icon="md-add">新增</Button>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="ExportSaleOrder()" icon="md-add">导出</Button>
                    </FormItem>
                </Form>
            </Card>
            <Table ref="table" style="margin-bottom: 10px" border show-summary:false :loading="loadingTable" :columns="tableTitle" :data="tableData" :height="tableHeight"></Table>
            <Page :total="total" :current="page" :page-size="size" :page-size-opts="[10, 50, 100, 300]" @on-change="(s) => {
                page = s;
                SearchSaleOrder();
            }
                " @on-page-size-change="(p) => {
        size = p;
        page = 1;
        SearchSaleOrder();
    }
        " show-total show-sizer />
            <Modal width="1200" :styles="{ top: '20px' }" :footer-hide="true" v-model="modalData" :title="modalTitle" :mask-closable="false" sticky draggable>
                <Saleorderedit v-if="modalData" :Id="tempDataId" :Type="tempType" @on-success="onSuccess"></Saleorderedit>
            </Modal>

            <iframe id="iframe-print" frameborder="0" style="display: none;" scrolling="auto" :src="printSrc"></iframe>
        </div>
    </div>
</template>

<script>
import {
    SearchSaleOrder,
    GetSaleOrderAndPrint,
    DeleteSaleOrder,
    CheckSaleOrder,
    ExportSaleOrder
} from '@/api/saleorder'
import Saleorderedit from './saleorderedit.vue'
export default {
    name: 'saleorder',
    components: { Saleorderedit },
    data() {
        return {
            Extras: {},
            printSrc: '/static/print/saleorder.html?temp=' + Date.parse(new Date()),
            total: 0,
            page: 1,
            size: 10,
            tableHeight: 450,
            sdate: null,
            edate: null,
            keyword: '',
            ruleValidate: {},
            loading: false,
            loadingTable: false,
            tempDataId: 0,
            tempType: 0,
            isPrint: 0,
            modalData: false,
            tableTitle: [
                {
                    type: 'index',
                    key: 'index',
                    title: '序号',
                    align: 'center',
                    fixed: 'left',
                    width: 80,
                    // render: (h, params) => {
                    //     return h('span', params.index + (this.page - 1) * this.size + 1)
                    // },
                    indexMethod: (params) => {
                        return params._index + (this.page - 1) * this.size + 1;
                    },
                    className: 'table-head-style',
                },
                {
                    title: '操作',
                    align: 'center',
                    fixed: 'left',
                    width: 220,
                    render: (h, params) => {
                        var operateArr = []
                        if (params.row.Status == 2 || params.row.Status == 3) {
                            operateArr.push(h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        var vm = this
                                        vm.tempDataId = params.row.Id
                                        vm.tempType = 0
                                        vm.modalData = true
                                    }
                                }
                            }, '修改'))
                        }
                        if (params.row.Status == 3 || params.row.Status == 0) {
                            operateArr.push(h('Button', {
                                props: {
                                    type: 'success',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.CheckSaleOrder(params.row.Id, 2)
                                    }
                                }
                            }, '提交审批'))
                        }
                        if (params.row.Status == 0) {
                            operateArr.push(h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.DeleteSaleOrder(params.row.Id)
                                    }
                                }
                            }, '删除'))
                        }
                        operateArr.push(h('Button', {
                            props: {
                                type: 'warning',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.GetSaleOrderAndPrint(params.row.Id)
                                }
                            }
                        }, '打印'))
                        return h('div', operateArr)
                    }
                },
                {
                    key: 'OrderDate',
                    title: '单据日期',
                    width: 120,
                    align: 'center',
                    render: (h, params) => {
                        return h('span', {}, this.dayjs(params.row.OrderDate).format('YYYY-MM-DD'))
                    }
                },
                {
                    key: 'OrderNo',
                    title: '单据编号',
                    width: 180,
                    align: 'center',
                    render: (h, params) => {
                        return h(
                            'a',
                            {
                                style: {
                                    fontSize: '16px',
                                    textDecoration: 'underline'
                                },
                                on: {
                                    click: () => {
                                        let vm = this
                                        vm.tempDataId = params.row.Id
                                        vm.tempType = 1
                                        vm.modalData = true
                                    }
                                }
                            },
                            params.row.OrderNo
                        )
                    }
                },
                {
                    key: 'CustomerName',
                    title: '姓名',
                    width: 100,
                    align: 'center'
                },
                {
                    key: 'Sex',
                    title: '性别',
                    width: 100,
                    align: 'center'
                },
                // {
                //     key: 'CustomerAge',
                //     title: '年龄',
                //     width: 100,
                //     align: 'center',
                // },
                {
                    key: 'DepartmentName',
                    title: '部门',
                    width: 100,
                    align: 'center'
                },
                {
                    key: 'Symptom',
                    title: '症状',
                    align: 'center',
                    minWidth: 200,
                    render: (h, params) => {
                        let texts = ''
                        if (params.row.Symptom !== null) {
                            if (params.row.Symptom.length > 25) {
                                texts = params.row.Symptom.substring(0, 25) + '...'
                            } else {
                                texts = params.row.Symptom
                            }
                        }
                        return h('Tooltip', {
                            props: {
                                placement: 'bottom'
                            }
                        }, [
                            texts,
                            h('span', {
                                slot: 'content',
                                style: {
                                    whiteSpace: 'normal',
                                    wordBreak: 'break-all'
                                }
                            }, params.row.Symptom)
                        ])
                    }
                },
                {
                    key: 'OrderDesc',
                    title: '药品',
                    align: 'center',
                    minWidth: 200,
                    render: (h, params) => {
                        let texts = ''
                        if (params.row.OrderDesc !== null) {
                            if (params.row.OrderDesc.length > 25) {
                                texts = params.row.OrderDesc.substring(0, 25) + '...'
                            } else {
                                texts = params.row.OrderDesc
                            }
                        }
                        return h('Tooltip', {
                            props: {
                                placement: 'bottom'
                            }
                        }, [
                            texts,
                            h('span', {
                                slot: 'content',
                                style: {
                                    whiteSpace: 'normal',
                                    wordBreak: 'break-all'
                                }
                            }, params.row.OrderDesc)
                        ])
                    }
                },
                {
                    key: 'Amount',
                    title: '合计(元)',
                    width: 120,
                    align: 'center'
                },
                // {
                //   key: 'PayFee',
                //   title: '付款金额(元)',
                //   width: 120,
                //   align: 'center'
                // },
                // {
                //     key: 'PrintNum',
                //     title: '打印次数',
                //     width: 100,
                //     align: 'center'
                // },
                {
                    key: 'Remark',
                    title: '临床诊断',
                    align: 'center',
                    minWidth: 200,
                    render: (h, params) => {
                        let texts = ''
                        if (params.row.Remark !== null) {
                            if (params.row.Remark.length > 25) {
                                texts = params.row.Remark.substring(0, 25) + '...'
                            } else {
                                texts = params.row.Remark
                            }
                        }
                        return h('Tooltip', {
                            props: {
                                placement: 'bottom'
                            }
                        }, [
                            texts,
                            h('span', {
                                slot: 'content',
                                style: {
                                    whiteSpace: 'normal',
                                    wordBreak: 'break-all'
                                }
                            }, params.row.Remark)
                        ])
                    }
                }
            ],
            tableData: [],
            allRoles: [],
            tempRoleIds: [],
            checkAll: false,
            tempDetail: [],
            modalChose: false,
            configData: {},
            dataProduct: []
        }
    },
    computed: {
        modalTitle() {
            if (this.tempType == 1) {
                return '处方单详情'
            } else {
                if (this.tempDataId > 0) {
                    return '修改处方单'
                } else {
                    return '新增处方单'
                }
            }
        }
    },
    methods: {
        onSuccess(res) {
            this.modalData = false
            if (res == 1) {
                this.SearchSaleOrder()
            }
        },
        ExportSaleOrder() {
            let vm = this
            if (vm.sdate != null && vm.sdate != '') {
                if (vm.sdate === 'object') {
                    vm.sdate = vm.dayjs(vm.sdate).format('YYYY年MM月DD日')
                }
            }
            else {
                vm.sdate = null;
            }
            if (vm.edate != null && vm.edate != '') {
                if (vm.edate === 'object') {
                    vm.edate = vm.dayjs(vm.edate).format('YYYY年MM月DD日')
                }
            } else {
                vm.edate = null;
            }
            var reqData = {
                sdate: vm.sdate,
                edate: vm.edate,
                keyword: vm.keyword
            }
            ExportSaleOrder(reqData).then((res) => {
                this.downloadfile(res)
            }).catch((err) => {
                vm.$Message.error({ title: '导出错误', content: err })
            })
        },
        downloadfile(res) {
            var blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
            var contentDisposition = res.headers['content-disposition']
            var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
            var filename = "处方单.xlsx"
            if (contentDisposition != undefined) {
                var result = patt.exec(contentDisposition)
                filename = result[1]
            }
            var downloadElement = document.createElement('a')
            var href = window.URL.createObjectURL(blob) // 创建下载的链接
            var reg = /^["](.*)["]$/g
            downloadElement.style.display = 'none'
            downloadElement.href = href
            downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
            document.body.appendChild(downloadElement)
            downloadElement.click() // 点击下载
            document.body.removeChild(downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL(href)
        },
        SearchSaleOrder() {
            let vm = this
            vm.loadingTable = true
            if (vm.sdate != null && vm.sdate != '') {
                if (vm.sdate === 'object') {
                    vm.sdate = vm.dayjs(vm.sdate).format('YYYY年MM月DD日')
                }
            }
            else {
                vm.sdate = null;
            }
            if (vm.edate != null && vm.edate != '') {
                if (vm.edate === 'object') {
                    vm.edate = vm.dayjs(vm.edate).format('YYYY年MM月DD日')
                }
            } else {
                vm.edate = null;
            }
            var reqData = {
                page: vm.page,
                size: vm.size,
                sdate: vm.sdate,
                edate: vm.edate,
                keyword: vm.keyword
            }
            SearchSaleOrder(reqData).then((res) => {
                vm.loadingTable = false
                var data = res.data
                if (data.StatusCode == 200) {
                    vm.tableData = data.Data.Items
                    vm.total = data.Data.TotalCount
                    vm.Extras = data.Extras
                } else {
                    vm.$Notice.error({
                        title: data.Errors
                    })
                }
            })
        },
        GetSaleOrderAndPrint(id) {
            GetSaleOrderAndPrint(id).then((res) => {
                const data = res.data
                if (data.StatusCode == 200) {
                    document
                        .getElementById('iframe-print')
                        .contentWindow.printData(data.Data)
                } else {
                    this.$Message.error({ title: '提示', content: data.Errors })
                }
            })
        },
        DeleteSaleOrder(id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确定要删除吗？',
                onOk: () => {
                    DeleteSaleOrder(id).then((res) => {
                        const data = res.data
                        if (data.StatusCode == 200) {
                            this.SearchSaleOrder()
                            this.$Message.success({ title: '提示', content: '删除成功' })
                        } else {
                            this.$Message.error({ title: '提示', content: data.Errors })
                        }
                    })
                },
                onCancel: () => { }
            })
        },
        CheckSaleOrder(id, status) {
            var reqData = {
                Id: id,
                Status: status
            }
            CheckSaleOrder(reqData).then((res) => {
                const data = res.data
                if (data.StatusCode == 200) {
                    this.SearchSaleOrder()
                    this.$Message.success({ title: '提示', content: '操作成功' })
                } else {
                    this.$Message.error({ title: '提示', content: data.Errors })
                }
            })
        },
        AddData() {
            this.tempDataId = 0
            this.tempType = 0
            this.modalData = true
        },

    },
    mounted() {
        this.tableHeight = window.innerHeight - 300
        //this.sdate = this.dayjs().subtract(1, 'month').date(1).format('YYYY年MM月DD日')
        //this.edate = this.dayjs().format('YYYY年MM月DD日')
        this.SearchSaleOrder()
    }

}
</script>
<style></style>
